package com.shsoft.component;

import com.shsoft.config.PCStatusConstant;
import com.shsoft.entity.HardWareInfo;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;

public class BarComponent extends BaseComponent{

    private String name;

    private int borderWidth;

    private String direction;

    private String style;

    private int arc;

    private int width;

    private int height;

    private int borderX,borderY;

    private int totalWidth,totalHeight;

    private double borderColorR,borderColorG,borderColorB,borderColorA;

    private double colorR,colorG,colorB,colorA;

    @Override
    public void drawComponent(GraphicsContext gc) {
        if(style.equals(PCStatusConstant.BAR_STYLE_1)){
            gc.setLineWidth(borderWidth * 2);
            if(direction.equals(PCStatusConstant.BAR_DIRECTION_V)){
                gc.setStroke(getBorderColor());
                gc.strokeRoundRect(getX(),getY(),height - borderWidth * 2,width - borderWidth * 2, arc, arc);
                gc.setFill(getColor());
                gc.fillRoundRect(getX() + borderWidth,getY(),height - borderWidth * 4,width / 2,arc,arc);
            }else {
                gc.setStroke(getBorderColor());
                gc.strokeRoundRect(getX(),getY(),width - borderWidth * 2,height - borderWidth * 2, arc, arc);
                gc.setFill(getColor());
                gc.fillRoundRect(getX(),getY() + borderWidth,width / 2,height - borderWidth * 4,arc,arc);
            }
            gc.setLineWidth(1);
        }else {
            if(direction.equals(PCStatusConstant.BAR_DIRECTION_V)){
                gc.setFill(getBorderColor());
                gc.fillRoundRect(getX(),getY(),height,width, arc, arc);
                gc.setFill(getColor());
                gc.fillRoundRect(getX(),getY(),height,width / 2,arc,arc);
            }else {
                gc.setFill(getBorderColor());
                gc.fillRoundRect(getX(),getY(),width,height, arc, arc);
                gc.setFill(getColor());
                gc.fillRoundRect(getX(),getY(),width / 2,height ,arc,arc);
            }
        }
    }

    @Override
    public void drawComponentToSmallCanvas(GraphicsContext gc) {
        if(style.equals(PCStatusConstant.BAR_STYLE_1)){
            gc.setLineWidth(borderWidth * 2);
            gc.setStroke(getBorderColor());
            gc.strokeRoundRect(borderWidth, borderWidth,totalWidth - borderWidth * 2,totalHeight - borderWidth * 2, arc, arc);
            if(direction.equals(PCStatusConstant.BAR_DIRECTION_V)){
                gc.setFill(getColor());
                gc.fillRoundRect(borderWidth * 2, borderWidth,height - borderWidth * 4, calculateLength(), arc, arc);
            }else {
                gc.setFill(getColor());
                gc.fillRoundRect(borderWidth, borderWidth * 2, calculateLength(),height - borderWidth * 4, arc, arc);
            }
            gc.setLineWidth(1);
        }else {
            gc.setFill(getBorderColor());
            gc.fillRoundRect(0, 0,totalWidth,totalHeight, arc, arc);
            gc.setFill(getColor());
            if(direction.equals(PCStatusConstant.BAR_DIRECTION_V)){
                gc.fillRoundRect(0, 0,height, calculateLength(), arc, arc);
            }else {
                gc.fillRoundRect(0, 0, calculateLength(),height, arc, arc);
            }
        }
    }

    @Override
    public void drawComponentBorder(Canvas canvas) {
        GraphicsContext gc = canvas.getGraphicsContext2D();
        gc.setStroke(Color.GREY);
        if(style.equals(PCStatusConstant.BAR_STYLE_1)){
            borderX = getX() - borderWidth;
            borderY = getY() - borderWidth;
        }else {
            borderX = getX();
            borderY = getY();
        }
        if(direction.equals(PCStatusConstant.BAR_DIRECTION_V)){
            totalWidth = height;
            totalHeight = width;
        }else {
            totalWidth = width;
            totalHeight = height;
        }
        gc.strokeRect(borderX, borderY, totalWidth, totalHeight);
    }

    @Override
    public boolean isMe(double x, double y) {
        if(x >= borderX && x <= borderX + totalWidth && y >= borderY && y <= borderY + totalHeight){
            return true;
        }
        return false;
    }

    private int calculateLength(){
        int totalLen;
        if(style.equals(PCStatusConstant.BAR_STYLE_1)){
            totalLen = width - borderWidth * 2;
        }else {
            totalLen = width;
        }
        if(name.equals(PCStatusConstant.COMPONENT_BAR_WEATHER_TEMP)){
            if(HardWareInfo.cityTemp >= 40){
                return totalLen;
            }else if(HardWareInfo.cityTemp <= -10){
                return 0;
            }else{
                return (HardWareInfo.cityTemp + 10) * totalLen / 50;
            }
        }else {
            int length = 0;
            if(name.equals(PCStatusConstant.COMPONENT_BAR_CPU_TEMP)){
                length = HardWareInfo.cpuTempCur;
            }else if (name.equals(PCStatusConstant.COMPONENT_BAR_CPU_USAGE)){
                length = HardWareInfo.cpuUsageCur;
            }else if (name.equals(PCStatusConstant.COMPONENT_BAR_GPU_TEMP)){
                length = HardWareInfo.gpuTempCur;
            }else if (name.equals(PCStatusConstant.COMPONENT_BAR_GPU_USAGE)){
                length = HardWareInfo.gpuUsageCur;
            }else if (name.equals(PCStatusConstant.COMPONENT_BAR_MEM_USAGE)){
                length = HardWareInfo.memUsed;
            }else if (name.equals(PCStatusConstant.COMPONENT_BAR_WEATHER_HUM)){
                length = HardWareInfo.cityHum;
            }
            if(length >= 100){
                return totalLen;
            }else {
                int i = length * totalLen / 100;
                if(i < 5){
                    i = 0;
                }
                return i;
            }
        }
    }

    public void setBorderColor(Color color) {
        this.borderColorR = color.getRed();
        this.borderColorG = color.getGreen();
        this.borderColorB = color.getBlue();
        this.borderColorA = color.getOpacity();
    }

    public Color getBorderColor() {
        return new Color(borderColorR,borderColorG,borderColorB,borderColorA);
    }

    public void setColor(Color color) {
        this.colorR = color.getRed();
        this.colorG = color.getGreen();
        this.colorB = color.getBlue();
        this.colorA = color.getOpacity();
    }

    public Color getColor() {
        return new Color(colorR,colorG,colorB,colorA);
    }

    public String getName() {
        return name;
    }

    public String getStyle() {
        return style;
    }

    public void setStyle(String style) {
        this.style = style;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getBorderWidth() {
        return borderWidth;
    }

    public void setBorderWidth(int borderWidth) {
        this.borderWidth = borderWidth;
    }

    public String getDirection() {
        return direction;
    }

    public void setDirection(String direction) {
        this.direction = direction;
    }

    public int getArc() {
        return arc;
    }

    public void setArc(int arc) {
        this.arc = arc;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getTotalHeight() {
        return totalHeight;
    }

    public void setTotalHeight(int totalHeight) {
        this.totalHeight = totalHeight;
    }

    public int getTotalWidth() {
        return totalWidth;
    }

    public void setTotalWidth(int totalWidth) {
        this.totalWidth = totalWidth;
    }

    public int getBorderX() {
        return borderX;
    }

    public void setBorderX(int borderX) {
        this.borderX = borderX;
    }

    public int getBorderY() {
        return borderY;
    }

    public void setBorderY(int borderY) {
        this.borderY = borderY;
    }
}
